/* Implements functionality for field validation. These functions are called from
   post processing functions implemented above. */


/*
 * Check if the field had negation in the validy check. If so return
 * negation of the result
 */
function checkNegationResult(fieldName, result) {
    srcTag = extractTag(fieldName);
    actionStr = eval('dzValidate_'+srcTag);
    if (actionStr[0] == '!')
        result = !result;

    return result;
} // checkNegationResult


/*
 * Validate a field. Validation is done whether using regular expression
 * or using remote call to script that analyses tha value of the field
 * and decides whether to accept it.
 * PARAMS:
 *  fieldName   The name of the field to validate
 * RETURNS:
 *  false If the value of the field is not accepted otherwise true
 */
function validateField(fieldName) {
    if (eval("document.NalleForm."+fieldName+".value") == '')
        return true;

    srcTag = extractTag(fieldName);
    actionStr = eval('dzValidate_'+srcTag);
    if (actionStr[0] == '!')
        actionStr = actionStr.substr(1, actionStr.length-1);

    limiter_index = actionStr.indexOf("://");
    action = actionStr.substr(0,limiter_index);
    if (action == 'rpc') {
        actionStr = actionStr.substr(limiter_index+3);
        if (actionStr.substring(actionStr.length-2, actionStr.length) == "()")
            actionStr = actionStr.substring(0, actionStr.length-2);
        /* Remote call defined */
        fieldName = "document.NalleForm." + fieldName;
        rCallStr = "accept," + fieldName + ".value,field" +
                   actionStr +"(" + fieldName + ".value,'"+fieldName+"')";
        doRCall(rCallStr, "validateRCallRes");
    } else if (action == 'reg') {
        /* Regular expression used to check validy */
        actionStr = actionStr.substr(limiter_index+3);
        validateRegExp = eval(actionStr);
        value = eval("document.NalleForm."+fieldName+".value");
        /* Create regular expression object used to validate field value */
        result = validateRegExp.test(value);

        return checkNegationResult(fieldName, result);
    } else if (action == 'table') {
        submitNalle("nalle", fieldName, 'EValidate', '');
    } else {
        /* List of constant values given */
        value = eval("document.NalleForm."+fieldName+".value");
        consts_arr = actionStr.split("|");
        c = consts_arr.length-1;
        result = false;

        while ((c >= 0) && (!result)) {
            if (value == consts_arr[c--])
                result = true;
        }

        return checkNegationResult(fieldName, result);
    }

    return true;
} // validateField


/*
 * Handle the result of the rcall. Related to validy check.
 * PARAMS:
 *  results     String containing the results from the remote script
 */
function validateRCallRes(results) {
    resultArr = results.split('/dz&');
    aCount = resultArr.length;
    /* This gets its value from the remote script it is assigned in the
       following for loop in the eval clause. If null the value was not
       acceptable so focus must be returned to the field */
    accept = 0;
    field = "";

    for (c = 0; c < aCount; c++) {
        eval(resultArr[c]);
    } // for

    field_elems = field.split(".");
    accept = checkNegationResult(field_elems[2], accept);

    if (!accept) {
        /* Only the field name. Not the whole access path. */
        ErrorField = field_elems[2];
        /* Value not accepted so return the focus */
        eval(field+'.focus()');
        /* Set flag that no need to check the validy in next blur event.
           This is gludge. It is needed to stop the infinite validy
           check loop that takes place, onBlur and OnFocus event
           are somehow mixed. */
        NextTimeDontCheck = true;
    }
}
